import homework9.Bean.Class;
import homework9.Bean.Student;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;

import java.util.List;

public class test {
    private static final SessionFactory FACTORY;

    static {
        //加载核心配置文件
        Configuration configuration = new Configuration();
        configuration.configure();
        //创建SessionFactory对象，并在数据库中把表创建出来
        FACTORY = configuration.buildSessionFactory();
    }

    public static Session getSession() {
        return FACTORY.openSession();
    }

    public static void main(String[] args) throws Exception {
        final Session session = getSession();
//        Class c = new Class();
//        c.setClassName("A班");
//        Student student = new Student();
//        student.setStuName("张三");
//        student.setStuNum("5");
//        student.setaClass(c);
//        c.getStudentList().add(student);
//
        Student student2 = new Student();
        student2.setStuName("李四");
        student2.setStuNum("6");
        Query query = session.createQuery("from Class where classID=1");
        List<Class> classList = query.list();
        Class c = classList.get(0);
        student2.setaClass(c);
        c.getStudentList().add(student2);

        session.save(c);
//        Student student = session.get(Student.class, 2);
//        session.delete(student);
        session.beginTransaction().commit();
    }
}
